﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using HJT212_Protocol_DAL.Model;

namespace HJT212_Protocol_DAL.DAO
{
    public class HostClientDao : BaseDao
    {
        /// <summary>
        /// 凭借MN号和密码，是否能够正确登录
        /// </summary>
        /// <param name="MN">MN号</param>
        /// <param name="PW">密码</param>
        /// <returns>是否可以正常登录</returns>
        public bool IsRightLogin(string MN, string PW)
        {
            try
            {
                if (string.IsNullOrEmpty(MN)) return false;
                string sqlSelect = " select top 1 MN, MCNName,PW from TB_HOSTCLIENTS WHERE MN='{0}' AND PW='{1}' ";
                sqlSelect = string.Format(sqlSelect, MN, PW);
                DataTable dt = helper.Data_Table(sqlSelect);
                if (dt != null && dt.Rows.Count > 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                log.Error("出现异常", ex);
            }
            return false;
        }


        /// <summary>
        /// 检索数据库中存储的客户端
        /// </summary>
        /// <returns></returns>
        public DataTable GetDbClients()
        {
            try
            {
                string sqlSelect = " select * from TB_HOSTCLIENTS ";
                DataTable dt = helper.Data_Table(sqlSelect);
                if (dt != null && dt.Rows.Count > 0)
                {
                    return dt;
                }
            }
            catch (Exception ex)
            {

                log.Error("出现异常", ex);
            }
            return null;
        }


        /// <summary>
        /// 检索数据库中存储的客户端
        /// </summary>
        /// <returns></returns>
        public List<HostClientModel> GetDbClientList()
        {
            try
            {
                DataTable dt = GetDbClients();
                if (dt != null)
                {
                    return HJ212_Common.DataConvert.DataTableToList<HostClientModel>(dt);
                }
            }
            catch (Exception ex)
            {
                log.Error("出现异常", ex);
            }
            return null;
        }


        /// <summary>
        /// 更新相应的MN的在线状态
        /// </summary>
        /// <param name="mn">数采仪唯一编码</param>
        /// <param name="State">状态</param>
        public void UpdateOnlineState(string mn, int State)
        {
            try
            {
                if (string.IsNullOrEmpty(mn) || (State != 0 && State != 1)) return;
                string sqlUpdate = "Update TB_HostClients Set ONLINESTATE={0} where MN='{1}' ";
                sqlUpdate = string.Format(sqlUpdate, State, mn);
                helper.ExecuteNonQuery(sqlUpdate);

            }
            catch (Exception ex)
            {
                log.Error("出现异常", ex);
            }
        }

        /// <summary>
        /// 更新所有的为不在线状态
        /// </summary>
        public void UpdateAllOffOnline()
        {
            try
            {
                string sqlUpdate = "Update TB_HostClients Set ONLINESTATE=0  ";
                helper.ExecuteNonQuery(sqlUpdate);
            }
            catch (Exception ex)
            {
                log.Error("出现异常", ex);
            }
        }

        /// <summary>
        /// 插入指定数据
        /// </summary>
        /// <param name="data">待插入的数据</param>
        public void InsertData(HostClientModel data)
        {
            try
            {
                #region SQl
                string sqlinsert = @" INSERT INTO 
                                      TB_HostClients
                                    (
                                      MN,
                                      MCNNAME,
                                      PW,
                                      ParentID,
                                      IsLeaf,
                                      ID
                                    ) 
                                    VALUES (
                                      '{0}',
                                      '{1}',
                                      '{2}',
                                      '{3}',
                                      {4},
                                      '{5}'
                                    ) ";
                #endregion
                sqlinsert = string.Format(sqlinsert, data.MN, data.MCNName, data.PW, data.ParentID, data.IsLeaf, data.ID);
                helper.ExecuteNonQuery(sqlinsert);
            }
            catch (Exception ex)
            {
                log.Error("出现异常", ex);
            }
            return;
        }

        /// <summary>
        /// 更新指定数据
        /// </summary>
        /// <param name="data">待更新的数据</param>
        public void UpdateData(HostClientModel data)
        {
            try
            {
                #region SQl
                string sqlinsert = @"  UPDATE 
                                  TB_HostClients  
                                SET 
                                  MN = '{0}',
                                  MCNNAME ='{1}',
                                  PW = '{2}',
                                  ONLINESTATE = '{3}',
                                  ParentID = '{4}',
                                  IsLeaf = {5} 
                                WHERE 
                                  ID = '{6}' ";
                #endregion
                sqlinsert = string.Format(sqlinsert, data.MN, data.MCNName, data.PW, data.OnLineState, data.ParentID, data.IsLeaf, data.ID);
                helper.ExecuteNonQuery(sqlinsert);
            }
            catch (Exception ex)
            {
                log.Error("出现异常", ex);
            }
            return;
        }
        /// <summary>
        /// 删除指定数据
        /// </summary>
        /// <param name="data">待删除的数据的ID</param>
        public void DelData(string ID)
        {
            try
            {
                #region SQl
                string sqlinsert = @"  delete  from  TB_HostClients
                                WHERE 
                                  ID = '{0}' ";
                #endregion
                sqlinsert = string.Format(sqlinsert, ID);
                helper.ExecuteNonQuery(sqlinsert);
            }
            catch (Exception ex)
            {
                log.Error("出现异常", ex);
            }
            return;
        }
        /// <summary>
        /// 根据MN检索数据，是否存在
        /// </summary>
        /// <param name="MN"> 指定的MN</param>
        public bool SearchDataByMn(string MN)
        {
            try
            {
                #region SQl
                string sql = @" select top 1 * from TB_HostClients where MN='{0}' ";
                #endregion
                sql = string.Format(sql, MN);
                DataTable dt = helper.Data_Table(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                log.Error("出现异常", ex);
            }
            return false;
        }
    }
}
